<?php
namespace app\admin\service;

use think\Loader;
use app\admin\service\Common;
use app\admin\model\MainIndic AS MainIndicModel;

class MainIndic extends Common {

    const MAININDIC_START = "https://datacenter.eastmoney.com/securities/api/data/get?type=RPT_F10_FINANCE_MAINFINADATA&sty=APP_F10_MAINFINADATA&filter=(";
    const MAININDIC_END = ")&p=1&ps=9&sr=-1&st=REPORT_DATE&source=HSF10&client=PC&v=";
    public function _initialize(){
    }

    public static function Collect($stockId = 0, $arr = []){
        $reMainIndic = "";

        $reMainIndic = file_get_contents(self::MAININDIC_START.urlencode('SECUCODE="'.$arr['stock_code'].'.'.$arr['jys'].'"').self::MAININDIC_END);
        if(!empty($reMainIndic)){
            $reMainIndic = json_decode($reMainIndic,true);
            $reMainIndic = $reMainIndic['result']['data'][0];

            //基本每股收益
            $data["one_base_earnings"] = 0;
            if(!empty($reMainIndic['EPSJB'])){
                $data["one_base_earnings"] = $reMainIndic['EPSJB'];
            }
            //每股净资产
            $data["net_asset_value_per_share"] = 0;
            if(!empty($reMainIndic['BPS'])){
                $data["net_asset_value_per_share"] = $reMainIndic['BPS'];
            }
            //每股公积金
            $data["one_provident_fund"] = 0;
            if(!empty($reMainIndic['MGZBGJ'])){
                $data["one_provident_fund"] = $reMainIndic['MGZBGJ'];
            }
            //每股未分配利润
            $data["one_undistributed_profits"] = 0;
            if(!empty($reMainIndic['MGWFPLR'])){
                $data["one_undistributed_profits"] = $reMainIndic['MGWFPLR'];
            }
            //每股经营现金流
            $data["one_business_cash_flow"] = 0;
            if(!empty($reMainIndic['MGJYXJJE'])){
                $data["one_business_cash_flow"] = $reMainIndic['MGJYXJJE'];
            }
            //营业总收入
            $data["total_operating_revenue"] = 0;
            if(!empty($reMainIndic['TOTALOPERATEREVE'])){
                $data["total_operating_revenue"] = $reMainIndic['TOTALOPERATEREVE'];
            }
            //毛利润
            $data["gross_profit"] = 0;
            if(!empty($reMainIndic['MLR'])){
                $data["gross_profit"] = $reMainIndic['MLR'];
            }
            //营业总收入同比增长率
            $data["year_on_year_growth_rate_total_operating_revenue"] = 0;
            if(!empty($reMainIndic['TOTALOPERATEREVETZ'])){
                $data["year_on_year_growth_rate_total_operating_revenue"] = $reMainIndic['TOTALOPERATEREVETZ'];
            }
            //营业总收入滚动环比增长率
            $data["rolling_month_on_growth_total_operating_revenue"] = 0;
            if(!empty($reMainIndic['PARENTNETPROFITTZ'])){
                $data["rolling_month_on_growth_total_operating_revenue"] = $reMainIndic['PARENTNETPROFITTZ'];
            }
            //净资产收益率
            $data["roe"] = 0;
            if(!empty($reMainIndic['ROEJQ'])){
                $data["roe"] = $reMainIndic['ROEJQ'];
            }
            //毛利率
            $data["gross_profit_rate"] = 0;
            if(!empty($reMainIndic['XSMLL'])){
                $data["gross_profit_rate"] = $reMainIndic['XSMLL'];
            }
            //净利率
            $data["net_profit_ratio"] = 0;
            if(!empty($reMainIndic['XSJLL'])){
                $data["net_profit_ratio"] = $reMainIndic['XSJLL'];
            }
            //预收账款/营业总收入比例
            $data["proportion_total_operat_revenue_advance_receipt"] = 0;
            if(!empty($reMainIndic['YSZKYYSR'])){
                $data["proportion_total_operat_revenue_advance_receipt"] = $reMainIndic['YSZKYYSR'];
            }
            //销售净现金流/营业总收入
            $data["net_cash_flow_sales_total_operating_revenue"] = 0;
            if(!empty($reMainIndic['XSJXLYYSR'])){
                $data["net_cash_flow_sales_total_operating_revenue"] = $reMainIndic['XSJXLYYSR'];
            }
            //经营净现金流/营业总收入
            $data["operating_net_cash_flow_total_operating_income"] = 0;
            if(!empty($reMainIndic['JYXJLYYSR'])){
                $data["operating_net_cash_flow_total_operating_income"] = $reMainIndic['JYXJLYYSR'];
            }
            //实际税率
            $data["actual_tax_rate"] = 0;
            if(!empty($reMainIndic['TAXRATE'])){
                $data["actual_tax_rate"] = $reMainIndic['TAXRATE'];
            }
            //流动比例
            $data["current_ratio"] = 0;
            if(!empty($reMainIndic['LD'])){
                $data["current_ratio"] = $reMainIndic['LD'];
            }
            //速动比例
            $data["quick_ratio"] = 0;
            if(!empty($reMainIndic['SD'])){
                $data["quick_ratio"] = $reMainIndic['SD'];
            }                        
            //现金流量比率
            $data["cash_flow_ratio"] = 0;
            if(!empty($reMainIndic['XJLLB'])){
                $data["cash_flow_ratio"] = $reMainIndic['XJLLB'];
            }                        
            //资产负债率
            $data["asset_liability_ratio"] = 0;
            if(!empty($reMainIndic['ZCFZL'])){
                $data["asset_liability_ratio"] = $reMainIndic['ZCFZL'];
            }                        
            //权益系数
            $data["equity_coefficient"] = 0;
            if(!empty($reMainIndic['QYCS'])){
                $data["equity_coefficient"] = $reMainIndic['QYCS'];
            }                        
            //产权比率
            $data["property_ownership_ratio"] = 0;
            if(!empty($reMainIndic['CQBL'])){
                $data["property_ownership_ratio"] = $reMainIndic['CQBL'];
            }                        
            //总资产周转天数(天)
            $data["total_asset_turnover_days"] = 0;
            if(!empty($reMainIndic['ZZCZZTS'])){
                $data["total_asset_turnover_days"] = $reMainIndic['ZZCZZTS'];
            }                        
            //存货周转天数(天)
            $data["inventory_turnover_days"] = 0;
            if(!empty($reMainIndic['CHZZTS'])){
                $data["inventory_turnover_days"] = $reMainIndic['CHZZTS'];
            }                        
            //应收账款周转天数(天)
            $data["days_sales_outstand"] = 0;
            if(!empty($reMainIndic['YSZKZZTS'])){
                $data["days_sales_outstand"] = $reMainIndic['YSZKZZTS'];
            }                        
            //总资产周转率(次)
            $data["total_asset_turnover_ratio"] = 0;
            if(!empty($reMainIndic['TOAZZL'])){
                $data["total_asset_turnover_ratio"] = $reMainIndic['TOAZZL'];
            }                        
            //存货周转率(次)
            $data["inventory_turnover_rate"] = 0;
            if(!empty($reMainIndic['CHZZL'])){
                $data["inventory_turnover_rate"] = $reMainIndic['CHZZL'];
            }                        
            //应收账款周转率(次)
            $data["accounts_receivable_turnover_ratio"] = 0;
            if(!empty($reMainIndic['YSZKZZL'])){
                $data["accounts_receivable_turnover_ratio"] = $reMainIndic['YSZKZZL'];
            }
            //净利润增长率
            $data["net_profit_growth_rate"] = 0;
            // if(!empty($reMainIndic[''])){
            //     $data["net_profit_growth_rate"] = $reMainIndic[''];
            // }                        
            //营业总收入增长率
            $data["growth_rate_total_operat_revenue"] = 0;
            // if(!empty($reMainIndic[''])){
            //     $data["growth_rate_total_operat_revenue"] = $reMainIndic[''];
            // }                        
            //公司债利率
            $data["liability_interest_rate"] = 0;
            // if(!empty($reMainIndic[''])){
            //     $data["liability_interest_rate"] = $reMainIndic[''];
            // }                        
            //是否增发
            $data["is_zfmx"] = 0;
            // if(!empty($reMainIndic[''])){
            //     $data["is_zfmx"] = $reMainIndic[''];
            // }                        
            //商誉净资产
            $data["net_goodwill"] = 0;
            // if(!empty($reMainIndic[''])){
            //     $data["net_goodwill"] = $reMainIndic[''];
            // }                        
            //股权质押
            $data["equity_pledge"] = 0;
            // if(!empty($reMainIndic[''])){
            //     $data["equity_pledge"] = $reMainIndic[''];
            // }                        
            //平均成本
            $data["average_cost"] = 0;
            // if(!empty($reMainIndic[''])){
            //     $data["average_cost"] = $reMainIndic[''];
            // }
            $reVal = 0;
            if(MainIndicModel::where(['sid'=>$stockId])->find()){
                $reVal = MainIndicModel::where(['sid'=>$stockId])->update($data);
            }else{
                $data['sid'] = $stockId;
                $reVal = MainIndicModel::insert($data);
            }
        }
        return $reVal;
    }
}